package com.bejoy.coolatin;

import java.util.ArrayList;

import cn.jpush.android.api.JPushInterface;

import com.android.volley.toolbox.ImageLoader;
import com.bejoy.coolatin.adapter.CommonViewPaperAdapter;
import com.bejoy.coolatin.custom.ZoomImageView;
import com.bejoy.coolatin.util.StaticProperty;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.AnimationDrawable;

public class WelcomeActivity extends Activity implements OnClickListener {
	SharedPreferences share = null;
	SharedPreferences.Editor sedit = null;
	private ImageView logo_view;
	private ViewPager viewpager_welcome;
	private com.bejoy.coolatin.adapter.CommonViewPaperAdapter viewPaperAdapter;
	private ArrayList<View> viewList;
	private int[] image = { R.drawable.ic_launcher, R.drawable.ic_launcher,
			R.drawable.ic_launcher, R.drawable.ic_launcher };
	private ImageView ensure_button, background_view;
	private int alpha = 255;
	float arg123;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);
		share = WelcomeActivity.this.getSharedPreferences(
				StaticProperty.SAVEINFO, Activity.MODE_PRIVATE);
		intiview();

		if (share.getString(StaticProperty.HAVELOGIN, "").equals("255")) {
			logo_view.setVisibility(View.VISIBLE);
//			RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
//					RelativeLayout.LayoutParams.FILL_PARENT,
//					RelativeLayout.LayoutParams.WRAP_CONTENT);
//			layoutParams.width = share.getInt(StaticProperty.SCREENWIDTH, 480);
//			logo_view.setLayoutParams(layoutParams);
			background_view.setVisibility(View.GONE);
			Alpha_Translate(logo_view, true);
		} else {
			sedit = share.edit();
			sedit.putString(StaticProperty.HAVELOGIN, "255");
			sedit.commit();
			background_view.setVisibility(View.VISIBLE);
			viewpager_welcome.setVisibility(View.VISIBLE);
			initviewpager();
		}
	}

	private void initviewpager() {
		viewList = new ArrayList<View>();
		// 加载viewpager
		AbsListView.LayoutParams ImageParams = new AbsListView.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT);
		for (int i = 0; i < image.length; i++) {
			ImageView myImageView = new ImageView(this);
			myImageView.setPadding(0, -50, 0, 0);
			myImageView.setImageResource(image[i]);
			myImageView.setLayoutParams(ImageParams);
			viewList.add(myImageView);
		}
		viewPaperAdapter = new com.bejoy.coolatin.adapter.CommonViewPaperAdapter(
				this, viewList);
		viewpager_welcome.setAdapter(viewPaperAdapter);
		viewpager_welcome.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				arg123 = 0;
				background_view.setVisibility(View.VISIBLE);
				Alpha_Translate(background_view, false);
				if (arg0 == 0) {
					background_view
							.setImageResource(R.drawable.blueback_ground);
					ensure_button.setVisibility(View.GONE);
				} else if (arg0 == 1) {
					background_view
							.setImageResource(R.drawable.darkblue_background);
					ensure_button.setVisibility(View.GONE);
				} else if (arg0 == 2) {
					background_view
							.setImageResource(R.drawable.darkblue_background);
					ensure_button.setVisibility(View.GONE);
				} else if (arg0 == 3) {
					background_view.setImageResource(R.drawable.bluesky_ground);
					ensure_button.setVisibility(View.VISIBLE);

				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				System.out.println("!===arg0" + arg0);
			}
		});
		// touch_flag++;
		// background_view.setAlpha(event.getRawX()/10*5);

		// background_view.setAlpha(alpha);
		// }else if (event.getRawX()>150) {
		// background_view.setAlpha(alpha-20);
		// }else if (event.getRawX()>200) {
		// background_view.setAlpha(alpha-30);
		// }else if (event.getRawX()>250){
		// background_view.setAlpha(alpha-40);
		// }else if (event.getRawX()>300) {
		// background_view.setAlpha(alpha-50);
		// }
	}

	private void intiview() {
		logo_view = (ImageView) findViewById(R.id.logo_view);
		ensure_button = (ImageView) findViewById(R.id.ensure_button);
		viewpager_welcome = (ViewPager) findViewById(R.id.viewpager_welcome);
		background_view = (ImageView) findViewById(R.id.background_view);
		ensure_button.setOnClickListener(this);
	}

	public void Alpha_Translate(final ImageView textViews, boolean isfirtenter) {
		final boolean isfirst = isfirtenter;
		AnimationSet animationSet = new AnimationSet(true);
		AlphaAnimation alphaAnimation = null;
		if (isfirtenter == true) {
			alphaAnimation = new AlphaAnimation(1, 0);
		} else if (isfirtenter != true) {
			alphaAnimation = new AlphaAnimation(0, 1);
		}
		alphaAnimation.setDuration(1500);
		animationSet.addAnimation(alphaAnimation);
		// TranslateAnimation translateAnimation = new TranslateAnimation(
		// Animation.RELATIVE_TO_SELF, 0.0f, // X轴的开始位置
		// Animation.RELATIVE_TO_SELF, 0.0f, // X轴的结束位置
		// Animation.RELATIVE_TO_SELF, 0.0f, // Y轴的开始位置
		// Animation.RELATIVE_TO_SELF, -10.0f); // Y轴的结束位置
		// translateAnimation.setDuration(3000);
		// animationSet.addAnimation(translateAnimation);
		textViews.startAnimation(animationSet);
		animationSet.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// background_view.setVisibility(View.GONE);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				if (isfirst == true) {
					logo_view.setVisibility(View.GONE);
					startActivityForResult(new Intent(WelcomeActivity.this,
							MainActivity.class), 1);
				}
			}
		});

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		JPushInterface.onResume(this);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		JPushInterface.onPause(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ensure_button:
			startActivityForResult(new Intent(this, MainActivity.class), 1);
			break;

		default:
			break;
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) {
		case 280:
			finish();
			break;

		default:
			break;
		}
	}
}
